package cn.touna.jss.modules.third.zxt;

import cn.touna.risk.api.contraints.facet.First;
import cn.touna.risk.api.enums.SystemErrorCodeEnum;
import cn.touna.jss.common.factory.ResponseFactory;
import cn.touna.jss.common.properties.disconf.ZengxintongProperties;
import cn.touna.jss.common.util.ValidUtil;
import cn.touna.jss.modules.adapter.ApiMapping;
import cn.touna.jss.modules.third.zxt.entity.ZengxintongEntity;
import cn.touna.jss.modules.third.zxt.util.AESUtil;
import cn.touna.risk.api.domain.Response;
import cn.touna.risk.api.http.HttpsUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpStatus;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * \* User: MeiZhongHao
 * \* Date: 2018-05-07
 * \* Time: 11:47
 * \* Description:
 * \
 */
@Service
public class ZengxintongService {

    private final static Logger logger = LogManager.getLogger(ZengxintongService.class);
    @Autowired
    private ZengxintongProperties zengxintongProperties;
    @Autowired
    private AESUtil aesUtil;
    // 三方接口返回状态码key
    private final static String STATUS_CODE = "status_code";
    // 三方接口返回值key
    private final static String DATA = "data";
    // 三方接口返回状态码说明key
    private final static String MSG = "msg";
    // 三方接口请求账户key
    private final static String USERNAME = "username";
    // 三方接口请求账户密码key
    private final static String PASSWORD = "password";
    // 三方接口请求参数key
    private final static String PARAMS = "params";

    @ApiMapping(value="zxt.propertyVerify", desc="增信通.获取房产验证数据")
    public Response estateVerify(ZengxintongEntity zengxintongEntity){
        //校验
        Response validResponse = ValidUtil.valid(zengxintongEntity, First.class);
        if (validResponse != null)
            return validResponse;
        // 参数模板
        String paramSrc = "{\"id_no\":\"%s\",\"protocol_data_type_name\":[\"%s\"],\"name\":\"%s\",\"mobile_no\":\"%s\"}";
        // 替换获得请求参数
        String paramStr = String.format(paramSrc,zengxintongEntity.getIdCard(),zengxintongProperties.getProtocol_data_type_name(),
                zengxintongEntity.getName(),zengxintongEntity.getMobile());

        Map<String, String> paramMap = new HashMap<>();
        paramMap.put(USERNAME,zengxintongProperties.getUsername());
        // 加密密码
        paramMap.put(PASSWORD, aesUtil.encrypt(zengxintongProperties.getPassword()));
        // 加密请求参数
        paramMap.put(PARAMS,aesUtil.encrypt(paramStr));

        try {
            String res = HttpsUtil.getInstance().post(zengxintongProperties.getUrl(), paramMap);
            JSONObject result = JSONObject.parseObject(res);
            // 若请求返回200则请求成功
            if (result.getInteger(STATUS_CODE) != null &&
                    result.getInteger(STATUS_CODE).equals(HttpStatus.SC_OK)) {
                return ResponseFactory.success(result);
            }
            return ResponseFactory.fail(SystemErrorCodeEnum.JSS_EXTSYSTEM_RESP_ERROR,result.getString(MSG));
        }catch (Exception e){
            return ResponseFactory.fail(SystemErrorCodeEnum.JSS_CALL_EXTSYSTEM_ERROR,e.getMessage());
        }
    }

}